package com.fd.finder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class PeoplePage extends Activity implements SensorEventListener,
		OnClickListener {

	private static HashMap<String, People> peopleDict = null;

	private SensorManager sensorManager;
	private List<Sensor> sensors;
	private Sensor sensor;
	private long lastUpdate = -1;
	private long currentTime = -1;

	private float last_x, last_y, last_z;
	private float current_x, current_y, current_z, currenForce;
	private static final int FORCE_THRESHOLD = 900;
	private final int DATA_X = SensorManager.DATA_X;
	private final int DATA_Y = SensorManager.DATA_Y;
	private final int DATA_Z = SensorManager.DATA_Z;
	private TextView name;
	private TextView tel;
	private TextView address;
	private TextView email;
	private String phoneNum;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.people);
		this.name = (TextView) findViewById(R.id.name);
		this.tel = (TextView) findViewById(R.id.tel);
		this.address = (TextView) findViewById(R.id.address);
		this.email = (TextView) findViewById(R.id.email);

		SensorManager theSensorService = (SensorManager) this
				.getSystemService(Context.SENSOR_SERVICE);
		this.sensorManager = theSensorService;
		this.sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors.size() > 0) {
			sensor = sensors.get(0);
		}

		View aboutButton = this.findViewById(R.id.call);
		aboutButton.setOnClickListener(this);

		Intent intent = getIntent();

		String id = intent.getStringExtra("name");
		People people = getInform(id);
		this.name.setText("Name: " + people.getName());
		this.tel.setText("Tel: " + people.getTel());
		this.address.setText("Address: " + people.getAdress());
		this.email.setText("Email: " + people.getEmail());
		phoneNum = people.getTel();
	}

	private People getInform(String id) {
		synchronized (PeoplePage.class) {
			if (peopleDict == null) {
				initDict();
			}
		}
		People people = null;
		if (peopleDict.containsKey(id)) {
			people = peopleDict.get(id);
		}
		return people;
	}

	private static void initDict() {
		File file = new File("/sdcard/data.txt");
		peopleDict = new HashMap<String, People>();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			String line = reader.readLine();
			while (line != null) {
				String[] segs = line.split("\\^\\~");
				People people = new People(segs[0].trim(), segs[1].trim(),
						segs[2].trim(), segs[3].trim());
				peopleDict.put(segs[0].trim(), people);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	public void start() {
		if (sensor != null) {
			sensorManager.registerListener(this, sensor,
					SensorManager.SENSOR_DELAY_GAME);
		}
	}

	public void stop() {
		sensorManager.unregisterListener(this);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER
				|| event.values.length < 3)
			return;

		currentTime = System.currentTimeMillis();

		if ((currentTime - lastUpdate) > 100) {
			long diffTime = (currentTime - lastUpdate);
			lastUpdate = currentTime;

			current_x = event.values[DATA_X];
			current_y = event.values[DATA_Y];
			current_z = event.values[DATA_Z];

			currenForce = Math.abs(current_x + current_y + current_z - last_x
					- last_y - last_z)
					/ diffTime * 10000;

			if (currenForce > FORCE_THRESHOLD) {
				Intent myIntentDial = new Intent(Intent.ACTION_CALL, Uri
						.parse("tel:" + phoneNum));

				startActivity(myIntentDial);
			}
			last_x = current_x;
			last_y = current_y;
			last_z = current_z;

		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.call:
			Intent myIntentDial = new Intent(Intent.ACTION_CALL, Uri
					.parse("tel:" + phoneNum));

			startActivity(myIntentDial);
			break;

		}
	}
}
